﻿/// <reference path="_namespace.js" />
/// <reference path="WinForm.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2007.
Class: ajax.windows.FrmJsObjectTreeViewer
/*****************************************************************************/

ajax.lang.addNamespace("ajax.windows");

//-----------------------------------------------------------------------------
// Constructor
ajax.windows.FrmJsObjectTreeViewer = function(doc, sLayerName, w, h, displayFlags, sText, parentNode, bModal, taskBar, displayNamespaces)
{    
    ajax.windows.FrmJsObjectTreeViewer.superConstructor.apply(this, arguments);
    
    // At the very least, start with Ajax
    this.displayNamespaces  = typeof(displayNamespaces) != "undefined" ? displayNamespaces : ["ajax"];    
    // Add on any registered apps
    for (var i = 0; i < ajax.registeredApps.length; i++)
        this.displayNamespaces.append(ajax.registeredApps[i], true);
        
    this._tree              = null;
    this._rhViewer          = null;
    this._splitter          = null;
    
    // We want to do stuff when we load
    this.getEvent(this.window, "onload").addListener(this.onLoadHandler);
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.windows.FrmJsObjectTreeViewer", "ajax.windows.WinForm"); // Inherit
//-----------------------------------------------------------------------------

///<property name="displayNamespaces" type="Array"/>
///<summary name="displayNamespaces">Add you application namespaces here as strings</sumary>
ajax.windows.FrmJsObjectTreeViewer.prototype.displayNamespaces = [];

//-----------------------------------------------------------------------------
ajax.windows.FrmJsObjectTreeViewer.prototype.onLoadHandler = function(e)
{
    // Create Tree
    this._tree = ajax.lang.safeNew("ajax.components.Treeview", this.doc, this.content, "", 0, 0, this.treeCallback.bind(this));
    // Style the tree
    this._tree.layerStyle.border            = "inset 1px ButtonFace";
    this._tree.layerStyle.backgroundColor   = "White";
    this._tree.checkboxes                   = false;
    // Set event handler
    this._tree.getEvent(this.window, "onNodeSelected").addListener(this._onTreeNodeSelected.bind(this));
    this._tree.load();
    
    
    // Create menus
    var ddmTree = ajax.lang.safeNew("ajax.components.Menu", this.doc, "", 150, 40, this.layer);
    ddmTree.animated = true;
    var ev = ajax.lang.getEvent(this.window, this._tree.layer, "oncontextmenu");
    ev.returnFalse = true;
    ev.addListener(ddmTree.showEvent.bind(ddmTree));
    var ddmTree_createInstance = ajax.lang.safeNew("ajax.components.MenuItem", this.doc, "Create Instance");
    ddmTree_createInstance.getEvent(this.window, "onSelected").addListener(function(e)
        {
            // If a tree node is selected, make a new instance of the Type in question
            // And set that as the namespace of a new instance of this class.
            var sNode = this._tree.selectedNode;
            if (sNode == null)
                return;
            
            var dataItem = sNode.dataItem;            
            var newWin = ajax.lang.safeNew("ajax.windows.FrmJsObjectTreeViewer", 
                this.doc, "", 640, 480, 0, "Instance of " + dataItem.key, this.innerCanvas, false, null, ["new " + dataItem.key + "()"]);
            newWin.show();
            
        }.bind(this)
    );
    ddmTree.addMenuItem(ddmTree_createInstance);
    ajax.lang.getEvent(this.window, this.doc.body, "onmouseup").addListener(ddmTree.hide.bind(ddmTree));
    
    // Refresh option
    var ddmTree_Refresh = ajax.lang.safeNew("ajax.components.MenuItem", this.doc, "Refresh Node", ajax.getDomainPath() + "/images/refresh16.gif");
    ddmTree_Refresh.getEvent(this.window, "onSelected").addListener(function(e, router)
        {
            this._tree.refreshNode(this._tree.selectedNode);
        }.bind(this)
    );
    ddmTree.addMenuItem(ddmTree_Refresh);
    
    
    // Create Viewer
    this._rhViewer = ajax.lang.safeNew("ajax.components.Container", this.doc, this.doc.body, "", 0, 0);
    
    // Create the Splitter
    var splitterTypes = ajax.lang.safeCall("ajax.components.Splitter", "getSplitterTypes");
    var rect          = this.getContentRect();
    this._splitter = ajax.lang.safeNew("ajax.components.Splitter", this.doc, this.content, "", rect.width, rect.height, splitterTypes.Vertical);
    // The splitter needs to respond to the us getting resized
    this.getEvent(this.window, "onResize").addListener(function(e)
        {
            var rect = this.getContentRect();
            this._splitter.setSize(rect.width, rect.height);
        }.bind(this)
    );
    
    // Add the frames to the splitter
    this._splitter.addFrame(this._tree);
    this._splitter.addFrame(this._rhViewer);
    
    // Show the splitter
    this._splitter.show(0, 0);
    // Start the tree off
    this._tree.loadItems(null);
    
}

//-----------------------------------------------------------------------------
// This fires when a tree node is selected
ajax.windows.FrmJsObjectTreeViewer.prototype._onTreeNodeSelected = function(e)
{
    this.setStatusText(ajax.lang.toJsonString(e.node.dataItem));
    try
    {
        this._rhViewer.layer.innerHTML = "<pre>" + ajax.lang.htmlEncode(e.node.dataItem.key.toJsonObject().toString()) + "</pre>";    
    }
    catch(ex)
    {
        ajax.debug.Output.writeLn(ex.message);
    }   
}

//-----------------------------------------------------------------------------
ajax.windows.FrmJsObjectTreeViewer.prototype.treeCallback = function(e)
{
    var ret     = new Array();
    var data    = e.dataItem;    
    data        = ajax.components.Treeview.Item.cast(data);
    
    if (!data)
    {
        // load default
        for (var i = 0; i < this.displayNamespaces.length; i++)
        {
            ret[i] = ajax.lang.safeNew("ajax.components.Treeview.Item", "Folder", "", 
                this.displayNamespaces[i], this.displayNamespaces[i]);
        }
    }
    else
    {        
        var obj = data.key.toJsonObject();        
        // ie does not add the prototype property of an object, so do that manually here (as the first item in the list)
        if (this.getDocument().all)
            if (ajax.lang.toJsonObject(data.key + "." + "prototype"))            
                ret[ret.length] = $new("ajax.components.Treeview.Item", "Folder", "", "prototype", data.key + "." + "prototype");
                
        for (var prop in obj)
        {
            // Cater for arrays
            if (ajax.lang.isNumeric(prop))
            {
                ret[ret.length] = ajax.lang.safeNew("ajax.components.Treeview.Item", "Folder", "", prop, data.key + "[" + prop + "]");
            }
            else
            {
                ret[ret.length] = ajax.lang.safeNew("ajax.components.Treeview.Item", "Folder", "", prop, data.key + "." + prop);
            }
        }        
    }
    return ret;
}

//-----------------------------------------------------------------------------
ajax.windows.FrmJsObjectTreeViewer.prototype.toString = function()
{
    return "ajax.windows.FrmJsObjectTreeViewer";
}

//-----------------------------------------------------------------------------
ajax.windows.FrmJsObjectTreeViewer.cast = function(obj)
{///<returns type="ajax.windows.FrmJsObjectTreeViewer"/>
    return obj;
}